<template>
  <el-dialog
    :title="!dataForm.time ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="ws10">
      <el-input v-model="dataForm.ws10" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="dir10">
      <el-input v-model="dataForm.dir10" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="ws30">
      <el-input v-model="dataForm.ws30" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="dir30">
      <el-input v-model="dataForm.dir30" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="ws50">
      <el-input v-model="dataForm.ws50" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="dir50">
      <el-input v-model="dataForm.dir50" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="ws70">
      <el-input v-model="dataForm.ws70" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="dir70">
      <el-input v-model="dataForm.dir70" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="wshub">
      <el-input v-model="dataForm.wshub" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="dirhub">
      <el-input v-model="dataForm.dirhub" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="temp">
      <el-input v-model="dataForm.temp" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="pressure">
      <el-input v-model="dataForm.pressure" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="rh">
      <el-input v-model="dataForm.rh" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="power">
      <el-input v-model="dataForm.power" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          time: 0,
          ws10: '',
          dir10: '',
          ws30: '',
          dir30: '',
          ws50: '',
          dir50: '',
          ws70: '',
          dir70: '',
          wshub: '',
          dirhub: '',
          temp: '',
          pressure: '',
          rh: '',
          power: ''
        },
        dataRule: {
          ws10: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dir10: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          ws30: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dir30: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          ws50: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dir50: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          ws70: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dir70: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          wshub: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dirhub: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          temp: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pressure: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          rh: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          power: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.time = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.time) {
            this.$http({
              url: this.$http.adornUrl(`/data/wind/info/${this.dataForm.time}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.ws10 = data.wind.ws10
                this.dataForm.dir10 = data.wind.dir10
                this.dataForm.ws30 = data.wind.ws30
                this.dataForm.dir30 = data.wind.dir30
                this.dataForm.ws50 = data.wind.ws50
                this.dataForm.dir50 = data.wind.dir50
                this.dataForm.ws70 = data.wind.ws70
                this.dataForm.dir70 = data.wind.dir70
                this.dataForm.wshub = data.wind.wshub
                this.dataForm.dirhub = data.wind.dirhub
                this.dataForm.temp = data.wind.temp
                this.dataForm.pressure = data.wind.pressure
                this.dataForm.rh = data.wind.rh
                this.dataForm.power = data.wind.power
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/data/wind/${!this.dataForm.time ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'time': this.dataForm.time || undefined,
                'ws10': this.dataForm.ws10,
                'dir10': this.dataForm.dir10,
                'ws30': this.dataForm.ws30,
                'dir30': this.dataForm.dir30,
                'ws50': this.dataForm.ws50,
                'dir50': this.dataForm.dir50,
                'ws70': this.dataForm.ws70,
                'dir70': this.dataForm.dir70,
                'wshub': this.dataForm.wshub,
                'dirhub': this.dataForm.dirhub,
                'temp': this.dataForm.temp,
                'pressure': this.dataForm.pressure,
                'rh': this.dataForm.rh,
                'power': this.dataForm.power
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
